package bbs.utils;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Resource;
import javax.imageio.ImageIO;

import bbs.entity.config.AppConfig;
import bbs.entity.constants.Constants;
import bbs.entity.enums.DateTimePatternEnum;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Component;

@Component("imageUtils")
public class ImageUtils {

    @Resource
    private AppConfig appConfig;

    public static Boolean createThumbnail(File file, int thumbnailWidth, int thumbnailHeight, File targetFile) {
        try {
            BufferedImage src = ImageIO.read(file);
//            缩率图高度
            int sorceW = src.getWidth();
            int sorceH = src.getHeight();
            if(sorceW <= thumbnailWidth) return false;

            int height = sorceH;
            if (sorceW > thumbnailWidth) {
                height = thumbnailWidth * sorceH / sorceW;
            } else {
                thumbnailWidth = sorceW;
                height = sorceH;
            }
//            缩率图宽度为150
            BufferedImage dst = new BufferedImage(thumbnailWidth, height, BufferedImage.TYPE_INT_BGR);
            Image scaled = src.getScaledInstance(thumbnailWidth, height, Image.SCALE_SMOOTH);
            Graphics2D g = (dst.createGraphics());
            g.drawImage(scaled, 0, 0, thumbnailWidth, height, null);
            g.dispose();

            int resultH = dst.getHeight();
            // 裁剪高度过高的图片
            if (resultH > thumbnailHeight) {
                resultH = thumbnailHeight;
                dst = dst.getSubimage(0, 0, thumbnailWidth, resultH);
            }
            ImageIO.write(dst, "jpg", targetFile);
            return true;
        }catch (IOException e){
            e.printStackTrace();
        }
        return false;
    }

    public String resetImageHtml(String paramString) {
        String month = DateUtil.format(new Date(), DateTimePatternEnum.YYYYMM.getPattern());
        List<String> list = getImageList(paramString);
        for (String img : list)
            resetImage(img, month);
        return month;
    }

    private String resetImage(String imagePath, String month) {
        if (StringTools.isEmpty(imagePath) || !imagePath.contains(Constants.FILE_FOLDER_TEMP_2))
            return imagePath;
        imagePath = imagePath.replace(Constants.READ_IMAGE_PATH, "");
        if (StringTools.isEmpty(month))
            month = DateUtil.format(new Date(), DateTimePatternEnum.YYYYMM.getPattern());
        String imageFileName = month + "/" + imagePath.substring(imagePath.lastIndexOf("/") + 1);
        File file = new File(this.appConfig.getProjectFolder() + Constants.FILE_FOLDER_FILE + Constants.FILE_FOLDER_IMAGE + imageFileName);
        try {
            FileUtils.copyFile(new File(this.appConfig.getProjectFolder() + Constants.FILE_FOLDER_FILE + imagePath), file);
        } catch (IOException iOException) {
            iOException.printStackTrace();
            return imagePath;
        }
        return imageFileName;
    }

    private List<String> getImageList(String paramString) {
        ArrayList<String> arrayList = new ArrayList<>();
        String regEx_img = "(<img.*src\\s*=\\s*(.*?)[^>]*?>)";
        Pattern pattern = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(paramString);
        while (matcher.find()) {
            String img = matcher.group();
            Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
            while (m.find()) {
                String imageUrl = m.group(1);
                arrayList.add(imageUrl);
            }
        }
        return arrayList;
    }
}
